﻿using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using System;

public class OutCard
{
    public static OutCard Create()
    {
        return new OutCard();
    }
    public List<GameObject> cards = null;
    private bool select_flag = false;
    private Transform cardRoot = null;
    public int index_start;
    public List<string> selectCard = new List<string>();
    public int cardNum = 0;
    public void Init(Transform group)
    {
        if (group == null)
        {
            Debug.LogError("object " + group + " is null ");
            return;
        }
        cardRoot = group;
        cards = new List<GameObject>();
        for (int i = 0; i < cardRoot.childCount; i++)
        {
            cards.Add(cardRoot.GetChild(i).GetChild(0).gameObject);
        }
        for (int i = 0; i < group.childCount; i++)
        {
            OutCardConsole.GetScript(group.GetChild(i).GetChild(0).gameObject).onPointerEnter += this.OnSelect;
            OutCardConsole.GetScript(group.GetChild(i).GetChild(0).gameObject).onPointerUp += this.OnEndSelect;
            OutCardConsole.GetScript(group.GetChild(i).GetChild(0).gameObject).onPointerDown += this.OnStartSelect;
        }
    }
    public List<GameObject> selectObj = null;
    private Color color = new Color(0.85f, 0.75f, 1f);
    private void OnStartSelect(GameObject go, PointerEventData data)
    {

        select_flag = true;
        if (go == null) return;
        index_start = cards.IndexOf(go);
        if (index_start == -1)
        {
            select_flag = false;
            return;
        }
        selectObj = new List<GameObject>();
        selectObj.Add(go);
        go.GetComponent<OutCardConsole>().ChangeColor(color);
    }
    private void OnSelect(GameObject go, PointerEventData data)
    {
        if (!select_flag) return;
        if (go == null) return;
        int index_e = cards.IndexOf(go);
        if (index_e == -1) OnEndSelect(go, data);
        int indexMin, indexMax;
        if (index_start <= index_e)
        {
            indexMin = index_start;
            indexMax = index_e;
        }
        else
        {
            indexMin = index_e;
            indexMax = index_start;
        }
        if (!cards.Contains(go))
        {
            for (int i = indexMin; i <= indexMax; i++)
            {
                if (selectObj.Contains(cards[i])) continue;
                else
                {
                    selectObj.Add(cards[i]);
                    cards[i].GetComponent<OutCardConsole>().ChangeColor(color);
                }
            }
        }
        else
        {
            foreach (var item in selectObj)
            {
                item.GetComponent<OutCardConsole>().ChangeColor(Color.white);
            }
            selectObj = new List<GameObject>();
            for (int i = indexMin; i <= indexMax; i++)
            {
                selectObj.Add(cards[i]);
                cards[i].GetComponent<OutCardConsole>().ChangeColor(color);
            }
        }
    }

    private void OnEndSelect(GameObject go, PointerEventData data)
    {
        if (!select_flag) return;
        select_flag = false;
        foreach (var item in selectObj)
        {
            item.GetComponent<OutCardConsole>().ChangeColor(Color.white);
            Move(item);
        }
        SendSelectCards(selectObj);
    }
    public void Move(GameObject go)
    {
        if (go == null) return;

        if (go.GetComponent<OutCardConsole>().isSelected)
        {
            MoveDown(go);
            go.GetComponent<OutCardConsole>().isSelected = false;
        }
        else
        {
            MoveUp(go);
            go.GetComponent<OutCardConsole>().isSelected = true;

        }
    }
    private void MoveUp(GameObject go)
    {
        go.transform.localPosition += Vector3.up * 30;
        if (!selectCard.Contains(go.name))
        {
            selectCard.Add(go.name);
        }
    }
    private void MoveDown(GameObject go)
    {
        go.transform.localPosition -= Vector3.up * 30;
        if (selectCard.Contains(go.name))
        {
            selectCard.Remove(go.name);
        }
    }
    public List<string> outCard = null;
    public void GetOutCard()
    {
        var num = 0;
        List<string> outCard = new List<string>();
        for (int i = 0; i < cardRoot.childCount; i++)
        {
            if (cardRoot.GetChild(i).gameObject.activeSelf == true)
            {
                var script = cardRoot.GetChild(i).GetChild(0).GetComponent<OutCardConsole>();
                if (script.isSelected)
                {
                    outCard.Add(cardRoot.GetChild(i).GetChild(0).gameObject.name);
                    num += 1;
                }
            }
        }
        cardNum = num;
        this.outCard = outCard;
    }

    private void SendSelectCards(List<GameObject> selectObj)
    {

        //for (int i = 0; i < selectObj.Count; i++)
        //{
        //    selectCard.Add(selectObj[i].name);
        //}
        //if (selectCard.Count>0)
        //    Util.CallMethod("WSKPlayersCtrl","GetOutCard",selectCard);
    }

    //private void MoveHnandCard(List<GameObject> selectCard)
    //{
    //    if (selectCard == null || selectCard.Count == 0) return;
    //    int a0 = selectCard.FindIndex(p => p.GetComponent<OutCardConsole>().isSelected == true);
    //    int a1 = selectCard.FindIndex(p => p.GetComponent<OutCardConsole>().isSelected == false);
    //    List<GameObject> cards = new List<GameObject>();
    //    if (a0 == -1)
    //        foreach (var item in selectCard)
    //        {
    //            cards.Add(item);
    //        }
    //    else if (a1 == -1)
    //        foreach (var item in selectCard)
    //        {
    //            item.GetComponent<OutCardConsole>().Move(item);
    //        }
    //    else
    //        foreach (var item in selectCard)
    //        {
    //            if (!item.GetComponent<OutCardConsole>().isSelected)
    //                item.GetComponent<OutCardConsole>().Move(item);
    //        }
    //    if (cards.Count == 0) return;

    //}
}

